﻿/*San Antonio Health Center Patient Information Management System
 * 
 * =====================================
 * Description: Add Member Page
 * 
 * -functionality which adds a member
 * that takes a check-up
 * -connects and saves in database
 * 
 * Notes:
 * -vaccine not yet linked
 * 
 * -------------------------------------
 * Date Finished: February 16, 2010
 * Author: Nikko Fernando O. Santillan
 * =====================================
 * */

/**
 * =====================================
 * Updated by: Monica DM. Tosino
 * Update(s): Graphical User Interface
 * Date Finished: March 1, 2010
 * =====================================
 * **/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace SAHCPIMS
{
    public partial class ADDMEMBER : Form
    {
        LOGIN logout = new LOGIN();

        public int employeeNumber;
        public string employeeName;
        public int familyNumber;

        public int doFlag = 0;

        public ADDMEMBER()
        {
            InitializeComponent();
        }

        public void putAccountInfo()
        {
            familyNumberMismo.Text = familyNumber.ToString();
            empNumLabel.Text = employeeNumber.ToString();
            employeeLabel.Text = employeeName;
        }

        private void addmemberbutton_Click(object sender, EventArgs e)
        {
            InitializeComponent();
        }

        private void logOutButton_Click(object sender, EventArgs e)
        {
            logout.Visible = true;
            this.Visible = false;
        }

        private void acceptMemberButton_Click(object sender, EventArgs e)
        {
            /** Connection String: Provider of the connection and the source of data **/
            string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\SAHCPIMS_database.mdb";

            /** create instance of the connection with the connection string as parameter **/
            OleDbConnection conn = new OleDbConnection(conString);

            /** 
             * Creating an instance of the DataSet which represents an in-memory cache of data
             * This is where the result of the database query will be stored
            **/
            DataSet ds = new DataSet();

            conn.Open();

            //for connection to the database
            try
            {

                OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into FamilyMember "
                                          + "values('" + familyNumber + "','" + memberFNameText.Text.ToString() + "','" + memberMNameText.Text.ToString() + "','" + memberSNameText.Text.ToString() + "','" + this.bdaybox.Text + "','" + int.Parse(weightTextBox.Text) + "','" + checkUpTypeCombo.Text.ToString() + "')", conn);

                tagalagay.Fill(ds);
                doFlag = 0;

            }
            //catches exception if the user input is invalid
            catch (FormatException)
            {

                memberVerificationLabel.Text = "Message: Invalid Input. Try Again.";
                doFlag = 1;

            }
            //catches exception if the input is already in the database or the input is not saved in the database
            catch (OleDbException)
            {
                memberVerificationLabel.Text = "Message: Data not Saved in Database. Data Already Exists.";
                doFlag = 1;

            }

            //closes database
            conn.Close();

            /** ====================== Added by: Monica Tosino :: March 2, 2010 ======================= **/
            if(doFlag == 0){
                this.fnum.Text = familyNumber.ToString();
                this.namelabel.Text = memberFNameText.Text + " " + memberMNameText.Text + " " + memberSNameText.Text;
                this.bdaylabel.Text = bdaybox.Text;
                this.weightlabel.Text = weightTextBox.Text + " kg";
                this.chkuplabel.Text = checkUpTypeCombo.Text;
                this.groupBox1.Visible = false;
                this.success.Visible = true;
            }

            }

        private void continuebutton_Click(object sender, EventArgs e)
        {
            //if registration is successful, the form is disabled
            if (doFlag == 0)
            {

                if (checkUpTypeCombo.Text.ToString() == "Prenatal")
                {
                    PRENATAL prenatalPage = new PRENATAL();
                    prenatalPage.familyNumber = familyNumber;
                    prenatalPage.firstName = memberFNameText.Text.ToString();
                    prenatalPage.middleName = memberMNameText.Text.ToString();
                    prenatalPage.surname = memberSNameText.Text.ToString();
                    prenatalPage.memberBDate = bdaybox.Text;
                    prenatalPage.weight = int.Parse(weightTextBox.Text.ToString());
                    prenatalPage.checkUpType = checkUpTypeCombo.Text.ToString();

                    prenatalPage.employeeName = employeeName;
                    prenatalPage.employeeNumber = employeeNumber;

                    prenatalPage.displayInformation();

                    prenatalPage.Visible = true;
                    this.Visible = false;
                }
                if (checkUpTypeCombo.Text.ToString() == "Vaccine")
                {
                    /** Editted by Monica Tosino :: February 26, 2010 **/
                    VACCINE vaccinePage = new VACCINE();
                    vaccinePage.fname = memberFNameText.Text.ToString();
                    vaccinePage.mName = memberMNameText.Text.ToString();
                    vaccinePage.sname = memberSNameText.Text.ToString();
                    vaccinePage.empName = employeeName;
                    vaccinePage.empNumber = employeeNumber;
                    vaccinePage.familyNo = familyNumber;
                    vaccinePage.bday = bdaybox.Text;
                    vaccinePage.weight = weightTextBox.Text + "kg";
                    vaccinePage.checkuptype = "Vaccine";
                    vaccinePage.displayInformation();

                    vaccinePage.Visible = true;
                    this.Visible = false;
                }
            }
        }


        private void addfamilybutton_Click(object sender, EventArgs e)
        {
            ADDFAMILY addFamilyForm = new ADDFAMILY();
            addFamilyForm.homeEmpName = employeeName;
            addFamilyForm.homeEmpNum = employeeNumber;
            addFamilyForm.putAccountInfo();
            addFamilyForm.Visible = true;
            this.Visible = false;
        }

        private void searchbutton_Click(object sender, EventArgs e)
        {
            SEARCH searchForm = new SEARCH();
            searchForm.employeeNumber = employeeNumber;
            searchForm.employeeName = employeeName;
            searchForm.putAccountInfo();
            searchForm.Visible = true;
            this.Visible = false;
        }
    }
}
